from datetime import datetime

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from module_admin.entity.do.business.sms_record_do import SmsRecord
from module_admin.entity.vo.business.sms_record_vo import SmsRecordPageQueryModel
from utils.page_util import PageUtil


class SmsRecordDao:
    """
    短信记录模块数据库操作层
    """

    @classmethod
    async def get_sms_record_dao(cls, db: AsyncSession, query_object: SmsRecordPageQueryModel, is_page: bool = False):
        """
        获取短信记录信息列表
        :param db: 数据库连接对象
        :param query_object: 查询对象
        :param is_page: 是否分页
        :return: 短信记录信息列表
        """
        query = select(SmsRecord).where(SmsRecord.del_flag == '0')

        # 动态生成查询语句

        query = query.order_by(
            SmsRecord.create_time.desc()
        ).distinct()

        sms_record_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)
        return sms_record_list

    @classmethod
    async def add_sms_record_dao(cls, db: AsyncSession, page_object: SmsRecord):
        """
        新增短信记录信息
        :param db: 数据库连接对象
        :param page_object: 新增对象
        :return: 新增结果
        """
        db_sms_record = SmsRecord(**page_object.model_dump())
        db.add(db_sms_record)
        await db.flush()

        return db_sms_record
